package com.Touch.TouchSimulator
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.TouchEvent;
	import flash.net.Socket;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	
	public class MultTouchControl extends Sprite
	{
		private var socket:Socket;
		
		private var ip:String;
		
		private var touch_list:Vector.<SimulateTouchObj>;
		
		private var w:int;
		
		private var h:int;
		
		private var reTry:int=0;
		
		private var maxReTry:int=10;
		
		public function MultTouchControl(_w:int,_h:int)
		{
			super();
			w=_w;
			h=_h;
			this.graphics.beginFill(0x000000,0);
			this.graphics.drawRect(0,0,w,h);
			this.graphics.endFill();
			socket=new Socket();
			touch_list=new Vector.<SimulateTouchObj>();
			Multitouch.inputMode=MultitouchInputMode.TOUCH_POINT;
		}
		
		/**
		 * 连接到远程服务器
		 */
		public function startTouch(_ip:String="127.0.0.1"):void{
			ip=_ip;
			socket.addEventListener(Event.CONNECT,on_connect);
			socket.addEventListener(Event.CLOSE,on_closed);
			socket.addEventListener(IOErrorEvent.IO_ERROR,on_error);
			startConnect();
		}
		
		//开始尝试连接
		private function startConnect():void{
			socket.connect(ip,763);
			reTry++;
		}
		
		private function on_error(event:IOErrorEvent):void{
			trace(event.toString());
		}
		
		//链接成功  开始监听触摸
		private function on_connect(event:Event):void{
			this.addEventListener(TouchEvent.TOUCH_BEGIN,on_touch_down);
			this.addEventListener(TouchEvent.TOUCH_MOVE,on_touch_move);
			this.addEventListener(TouchEvent.TOUCH_END,on_touch_end);
			this.addEventListener(Event.ENTER_FRAME,on_send);
			
			reTry=0;
		}
		
		//链接断开中断
		private function on_closed(event:Event):void{
			this.removeEventListener(TouchEvent.TOUCH_BEGIN,on_touch_down);
			this.removeEventListener(TouchEvent.TOUCH_MOVE,on_touch_move);
			this.removeEventListener(TouchEvent.TOUCH_END,on_touch_end);
			this.removeEventListener(Event.ENTER_FRAME,on_send);
			
			if(reTry>=maxReTry){
				return;
			}else{
				setTimeout(startConnect,1000);
			}
		}
		
		private function on_send(event:Event):void{
			var _xml:XML=new XML(<dt><dt>dt</dt></dt>);
			for each (var temp:SimulateTouchObj in touch_list){
				if(temp.isMoved){
					_xml.appendChild(temp.outputxml());
				}
			}
			for(var i:int=0;i<touch_list.length;i++){
				if(!touch_list[i].isExist){
					touch_list.splice(i,1);
				}
			}
			if(_xml.obj.length()>0){
				var xmlByte:ByteArray=new ByteArray();
				xmlByte.writeUTFBytes(_xml);
				xmlByte.compress();
				socket.writeBytes(xmlByte);
			}
		}
		
		
		
		/*                  状态                 */
		private function on_touch_down(event:TouchEvent):void{
			var temp:SimulateTouchObj=new SimulateTouchObj(event.touchPointID,event.localX,event.localY,w,h);
			touch_list.push(temp);
		}
		private function on_touch_move(event:TouchEvent):void{
			for each (var temp:SimulateTouchObj in touch_list){
				if(temp.touchID==event.touchPointID){
					temp.updata(event.localX,event.localY);
					return;
				}
			}
			//当未发现此id存在于列表则新建
			var temp_move:SimulateTouchObj=new SimulateTouchObj(event.touchPointID,event.localX,event.localY,w,h);
			touch_list.push(temp_move);
		}
		private function on_touch_end(event:TouchEvent):void{
			for each (var temp:SimulateTouchObj in touch_list){
				if(temp.touchID==event.touchPointID){
					temp.remove(event.localX,event.localY);
					return;
				}
			}
			//当未发现此id存在于列表则新建  并将其设置为移除项
			var temp_remove:SimulateTouchObj=new SimulateTouchObj(event.touchPointID,event.localX,event.localY,w,h);
			temp_remove.remove(event.localX,event.localY);
			touch_list.push(temp_remove);
		}
	}
}